package com.example.m2l_digicode;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;


public class MainActivity extends Activity {

	 
	 boolean connexion=false;
	
    @SuppressWarnings("deprecation")
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        final Button btn_connexion = (Button) findViewById(R.id.btn_connexion);
        
        
        connexion = isOnline();
        if(!connexion)
        {
        	final AlertDialog net_erreur = new AlertDialog.Builder(MainActivity.this).create();
            	net_erreur.setTitle("Erreur");
            	net_erreur.setMessage("Cette application requiert une connexion internet \n" +
            			"Veuillez activer une connexion");       
            	net_erreur.setButton("OK", new DialogInterface.OnClickListener() {
            		public void onClick(DialogInterface dialog, int which) {
            			net_erreur.dismiss();
            		} });
            	net_erreur.show();
            	
               final Button button_digi = (Button) findViewById(R.id.menu_btn_digi);
                button_digi.setEnabled(false);
                
                final Button button_wifi = (Button) findViewById(R.id.menu_btn_wifi);
                button_wifi.setEnabled(false);
                
                final Button button_demandeur = (Button) findViewById(R.id.menu_btn_demandeur);
                button_demandeur.setEnabled(false);
                
                btn_connexion.setVisibility(View.VISIBLE);
                
                btn_connexion.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                    	
                    	connexion=isOnline();
                    	if(connexion)
                    	{
                    		button_digi.setEnabled(true);
                    		button_wifi.setEnabled(true);
                    		button_demandeur.setEnabled(true);
                    		btn_connexion.setVisibility(View.GONE);
                    		btn_connexion.setEnabled(false);
                    	}
                    	
                    }  
                });
                

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
	public void afficherDigicode (View view) 
	{
	    Intent intent_digi = new Intent(this, Digicode.class);
	    startActivity(intent_digi);

	}
	
	public void afficherWifi (View view)
	{
		Intent intent_wifi = new Intent(this, Wifi.class);
		startActivity(intent_wifi);
		
	}
	public void login (View view)
	{
		Intent intent_login = new Intent(this, Login.class);
		startActivity(intent_login);
		
	}
	
	public boolean isOnline() {
	    ConnectivityManager cm =
	        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
	    NetworkInfo netInfo = cm.getActiveNetworkInfo();
	    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
	        return true;
	    }
	    return false;
	}

	
    
}
